package com.itheima.consumer.config;

import org.springframework.amqp.core.Binding;
import org.springframework.amqp.core.BindingBuilder;
import org.springframework.amqp.core.DirectExchange;
import org.springframework.amqp.core.Queue;
import org.springframework.amqp.rabbit.core.RabbitTemplate;
import org.springframework.amqp.rabbit.retry.RepublishMessageRecoverer;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

@Configuration
public class ErrorMessageConfiguration {
    @Bean
    public DirectExchange errorExchange() {
        return new DirectExchange("error.exchange", true, false);
    }
    @Bean
    public Queue errorQueue() {
        return new Queue("error.queue", true, false, false);
    }
    @Bean
    public Binding errorBinding(DirectExchange errorExchange,Queue errorQueue) {
//        return new Binding("error.queue", Binding.DestinationType.QUEUE, "error.exchange", "error.routing.key", null);
        return  BindingBuilder.bind(errorQueue).to(errorExchange).with("error.routing.key");
    }
    //指定死信重发队列
    @Bean
    public RepublishMessageRecoverer republishMessageRecoverer(RabbitTemplate rabbitTemplate) {
        return new RepublishMessageRecoverer(rabbitTemplate, "error.exchange", "error.routing.key");
    }
}
